﻿//
// Created by francklinson on 2021/3/28.
//

#include "Port.h"

Port::Port(const char *br, const char *st, int b)
{
    brand = new char[strlen(br) + 1];
    strcpy(brand, br);
    style = new char[strlen(st) + 1];
    strcpy(style, st);
    bottles = b;
}

Port::Port(const Port &p)
{
    brand = new char[strlen(p.brand) + 1];
    strcpy(brand, p.brand);
    style = new char[strlen(p.style) + 1];
    strcpy(style, p.style);
    bottles = p.bottles;
}

Port &Port::operator=(const Port &p)
{
    if (this == &p)
        return *this;
    std::cout << "Port copy" << std::endl;
    delete[] brand;         // prepare for new style
    brand = new char[strlen(p.brand) + 1];
    strcpy(brand, p.brand);
    delete[]style;
    style = new char[strlen(p.style) + 1];
    strcpy(style, p.style);
    bottles = p.bottles;
    return *this;
}

Port &Port::operator+=(int b)
{
    bottles += b;
    return *this;
}

Port &Port::operator-=(int b)
{
    bottles -= b;
    return *this;
}

void Port::Show() const
{
    cout << "Brand: " << brand << endl;
    cout << "Kind: " << style << endl;
    cout << "Bottles: " << bottles << endl;
}

ostream &operator<<(ostream &os, const Port &p)
{
    cout << p.brand << ", " << p.style << ", " << p.bottles;
    return os;
}

